package com.onehash.onebilling.entity;

import java.io.Serializable;

/*
 * Author : Xia Xi
 * 
 */
public class User implements Serializable {
	
	public enum EnumUserRole {
		ADMIN,
		AGENT,
		SUPERADMIN
	}
	private EnumUserRole role;
	private String userName;
	private String password;


    public User (EnumUserRole role, String userName, String password) {
    	
        this.role = role;
        this.userName = userName;
        this.password = encryptPassword(password);

    }    
    
    public EnumUserRole getRole() {
		return role;
	}



	public String getUserName() {
		return userName;
	}
	
	public void setRole(EnumUserRole role){
		this.role = role;
	}
	
	public void setPassword (String password) {
		this.password = encryptPassword(password);
		
	}
	

	public Boolean isCorrectPassword(String inputPassword) {
		if (inputPassword != null && inputPassword.length() > 0) {
			if (encryptPassword(inputPassword).equals(password)){
				return true;
			}
			
		}
		
		return false;
		
	}
	
	private String encryptPassword(String password) {
		//use md5 to encrypt
		//test calculate "a", the value should be 0cc175b9c0f1b6a831c399e269772661
		String encPass = getMD5(password.getBytes());
		return encPass;
		
	}
	
	 private String getMD5(byte[] source) {  
		  String s = null;  
		  char hexDigits[] = {       
		     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',  'e', 'f'};   
		   try  
		   {  
		    java.security.MessageDigest md = java.security.MessageDigest.getInstance( "MD5" );  
		    md.update( source );  
		    byte tmp[] = md.digest();            
		    char str[] = new char[16 * 2];   
		                                                  
		    int k = 0;                              
		    for (int i = 0; i < 16; i++) {         
		                                                 
		     byte byte0 = tmp[i];               
		     str[k++] = hexDigits[byte0 >>> 4 & 0xf];
		                                                         
		     str[k++] = hexDigits[byte0 & 0xf];           
		    }   
		    s = new String(str);                               
		  
		   }catch( Exception e )  
		   {  
		    e.printStackTrace();  
		   }  
		   return s;  
	}  

	public String toString () {
        return (role + "_" + userName );
    }

    public void show () {
        System.out.println (this);
    }
    
}
